package com.sy.common.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by filamoon on 11/19/14.
 */
@MappedSuperclass
@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class ReadOnlyBaseModel implements Serializable {
    //4258595655397633406, -8074419226097483397
    private static final long serialVersionUID = 4258595655397633406l;
    public final static String NOSQL_PRIMARY_COL_PID = "p";          // 分片键, 用户手机号后6位
    public final static String NOSQL_PRIMARY_COL_USER_ID = "uid";
    public final static String NOSQL_PRIMARY_COL_ID = "id";
    public final static String NOSQL_COL_CREATED = "ts";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long id = -1l;

    @Column(nullable = false, updatable = false)
//    protected Date create_time;

    @PrePersist
    protected void onCreate() {
//        if (create_time == null) {
//            create_time = new Date();
//        }
    }

    protected boolean onlyOneIsNull(Object a, Object b) {
        if (a == null && b != null) {
            log.warn("a is null");
            return true;
        }
        if (a != null && b == null) {
            log.warn("b is null");
            return true;
        }
        return false;
    }

    protected boolean bothAreNull(Object a, Object b) {
        return a == null && b == null;
    }

    protected boolean equalsWithNull(Object a, Object b) {
        if (bothAreNull(a, b)) return true;
        if (onlyOneIsNull(a, b)) return false;
        boolean r = a.equals(b);
        return r;
    }

    protected boolean equalsForDate(Date a, Date b) {
        if (bothAreNull(a, b)) return true;
        if (onlyOneIsNull(a, b)) return false;
        if (a.getTime() == b.getTime()) return true;
        return false;
    }
}
